package cn.com.dhcc.turbo.web.controller.exch;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.http.util.NetUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.annotation.SiNoNeedLogin;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.handler.remotehandler.socket.ICallBack;
import cn.com.dhcc.app.core.util.NetUtil;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.service.TurboFtpClientFactory;
import cn.com.dhcc.app.pub.core.util.FtpUtil;
import cn.com.dhcc.turbo.service.registry.sync.RmiSendDataClient;
import cn.com.dhcc.turbo.service.registry.sync.SyncCallBack;


@Controller
@RequestMapping("/test")
public class RmiTestController extends BaseController {
	@Autowired
	RmiSendDataClient sendDataClient;
	
	@RequestMapping("/rmi")
	@ResponseBody
	@SiNoNeedLogin
	public String rmi(String ip,String port,String content){
			final SyncCallBack backMsg = new SyncCallBack();
			try {
				System.out.println("ip【"+ip+"】"+"port【"+port+"】");
				if(content==null || content.equals("")){
					content="这是测试数据";
				}
				sendDataClient.sendData(ip, Integer.parseInt(port), content, new ICallBack() {
					@Override
					public void recevieCallBackData(Object callBackData) throws NiceException {
						SyncCallBack callBack = (SyncCallBack)callBackData;
						System.out.println("返回信息:"+callBack.getMsg());
						backMsg.setMsg("rmi调用成功，返回信息:"+callBack.getMsg());
					}
					@Override
					public void onException(Throwable cause) throws NiceException {
						backMsg.setMsg("rmi调用失败,失败信息"+cause.toString());
					}
				});
				return backMsg.getMsg();
			} catch (Exception e) {
			   return e.toString();
			}
			
	}
	
	
	@RequestMapping("/ftp")
	@ResponseBody
	@SiNoNeedLogin
	public String ftp(String ip,String port){
		FTPClient  client=TurboFtpClientFactory.createFtpClient();
		String tousername = AppConst.EMBFTP.EMBFTP_ADMIN_USERID;//发送目的服务器的ftp用户名
		String topassword;
		try {
			topassword = AppConst.EMBFTP.EMBFTP_ADMIN_PWD+" "+tousername+" "+AppConst.getAdminAuthCode();
			boolean flag=FtpUtil.login(client, ip, tousername, topassword, Integer.parseInt(port));
			if (flag) {
				
				client.enterLocalPassiveMode();
				String str="服务器返回:"+client.getReplyCode()+" "+client.getReplyString();
			    str=str+" "+client.listFiles().toString();
				str=str+" "+client.getReplyCode()+" "+client.getReplyString();
				return "登陆成功:"+str;
			}else{
				return "登陆失败";
			}
		} catch (Exception e1) {
			 return e1.toString()+" "+client.getReplyCode()+" "+client.getReplyString();
		}
	}
	
	/**
	 * 测试网络端口
	 * @param ip
	 * @param port
	 * @return
	 */
	@RequestMapping("/net")
	@ResponseBody
	@SiNoNeedLogin
	public String net(HttpServletRequest request,String ip,String port){
		String clientIp=NetUtil.getRemoteIp(request);
		boolean flag=NetUtil.canConnect(ip, Integer.parseInt(port));
		return "ping-"+ip+":"+port+"的结果:"+flag+",客户端IP:"+clientIp;
	}
}
